package com.pzh.guli.service.edu.controller.api;

import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.edu.pojo.AliPay;
import com.pzh.guli.service.edu.pojo.Order;
import com.pzh.guli.service.edu.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alipay.easysdk.factory.Factory;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("api/alipay")
public class AliPayController {
    @Autowired
    private IOrderService service;
    @GetMapping("pay")
    public String alipay(AliPay aliPay) throws Exception {
        //禁止重复支付
        QueryWrapper<Order> orderQueryWrapper=new QueryWrapper<>();
        orderQueryWrapper.eq("order_no",aliPay.getTraceNo());
        orderQueryWrapper.eq("status",1);//支付状态1 已支付
        long count = service.count(orderQueryWrapper);
        if (count!=0){
            return "PAYED(请勿重复支付)";//已支付
        }
        //校验金额是否正确
        QueryWrapper<Order> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("order_no",aliPay.getTraceNo());
        QueryWrapper.eq("total_fee",aliPay.getTotalAmount());//金额校验
        long flag = service.count(QueryWrapper);
        if (flag==0){
            return "小黑客你在想啥呢...............";
        }
        AlipayTradePagePayResponse response;
        response=Factory.Payment.Page()
                .pay(URLEncoder.encode(aliPay.getSubject(),"UTF-8"),aliPay.getTraceNo(),aliPay.getTotalAmount(),"");
        return response.getBody();
    }
    @PostMapping("callback")
    public String callback(HttpServletRequest request){
        if (request.getParameter("trade_status")==null){
            log.info("出现非法请求支付回调api");
            return "PAY IS ERROR";
        }
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")){
            Map<String,String> params=new HashMap<>();
            Map<String,String[]> requestParams=request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name,request.getParameter(name));
                //回调各种参数
            }
            String orderNo=params.get("out_trade_no");
            Integer updateStatus=1; //1 表示已支付
            //更新支付状态
            UpdateWrapper<Order> orderUpdateWrapper=new UpdateWrapper<>();
            orderUpdateWrapper.eq("order_no",orderNo);
            orderUpdateWrapper.set("status",updateStatus);
            service.update(orderUpdateWrapper);
        }
        return "SUCCESS";
    }
}
